package com.leyou.item.web;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.services.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 规格参数控制类
 * @Date 2018/12/15  16:48
 * @Author JIE
 */
@RestController
@RequestMapping("spec")
public class SpecificationController {


    @Autowired
    private SpecificationService specificationService;

    /**
     * 根据分类的id查询规格组
     * @param cid
     * @return
     */
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupByCid(@PathVariable("cid")Long cid){
        List<SpecGroup> specGroups = specificationService.queryGroupByCid(cid);
        return ResponseEntity.ok(specGroups);
    }

    /**
     * 添加规格组
     * @param cid
     * @param name
     * @return
     */
    @PostMapping("group")
    public  ResponseEntity<Void> saveSpecGroup(@RequestParam("cid") Long cid,@RequestParam("name") String name){
         specificationService.saveSpecGroup(cid,name);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组数据
     * @param group
     * @return
     */
    @PutMapping("group")
    public  ResponseEntity<Void> updateSpecGroup(SpecGroup group){
        specificationService.updateSpecGroup(group);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("group/{id}")
    public  ResponseEntity<Void> deleteSpecGroup(@PathVariable("id") Long id){
        specificationService.deleteSpecGroupById(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询参数的集合
     * @param gid
     * @param cid
     * @param searching
     * @return
     */
    @GetMapping("params")
    public  ResponseEntity<List<SpecParam>> queryParamList(@RequestParam(value = "gid",required = false)Long gid
             ,@RequestParam(value = "cid",required = false)Long cid
             ,@RequestParam(value = "searching",required = false)Boolean searching){
        List<SpecParam> specParams = specificationService.queryParamList(gid,cid,searching);
        return ResponseEntity.ok(specParams);
    }

    /**
     * 新增规格参数
     * @param specParam
     * @return
     */
    @PostMapping("param")
    public  ResponseEntity<Void> saveSpecParam(SpecParam specParam){
           specificationService.saveSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param specParam
     * @return
     */
    @PutMapping("param")
    public  ResponseEntity<Void> updateSpecParam(SpecParam specParam){
        specificationService.updateSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("param/{id}")
    public  ResponseEntity<Void> deleteSpecParam(@PathVariable("id")Long id){
         specificationService.deleteSpecParamById(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据分类的id查询参数组及其组内的参数
     * @param cid
     * @return
     */
    @GetMapping("/group")
    public ResponseEntity<List<SpecGroup>> queryListByCid(@RequestParam("cid")Long cid){
        List<SpecGroup> specGroups = specificationService.queryListByCid(cid);
        return ResponseEntity.ok(specGroups);
    }


}
